class PPlot
    def initialize
      @bases   = {} 
      @order   = []
    end

    def set_order(new_order)
        new_order.each do |o_check|
            if !@bases.keys.include?(o_check)
               raise "BASE \"#{o_check}\" NOT AVAILABLE"
            end
        end
        @order = new_order
    end

    def order
        @order
    end

    def get_base(name)
        @bases[name]
    end

    def add_base(base)
        @bases[ base.name ] = base
    end

    def display
        if @order.size > 0
           @order.each do |base_name|
               @bases[base_name].display
           end
        end
    end

    def get_ready
      calc_positions
    end

    def bar_to_s
        result      = ""
        partitioned = {}
        @bases.keys.each{ |x|
            type = @bases[x].type.to_s
            if !partitioned.has_key?(type) 
                partitioned[type] = [x] 
            else
                partitioned[type].push(x)
            end
        }
        partitioned.sort.each do |type, fields|
            result << "handler: #{type}\n+++++++++++++++++++++\n\t #{fields.sort.join("\n\t")}\n\n"
        end
        result
    end

    def line_list
        @bases.keys
    end

    private
    def calc_positions
        if @bases.values.size > 0
           slice = (1.0/@order.size)
           @order.each_with_index do |base_name, index|
               @bases[base_name].x = slice*index
           end
        end
    end
end

PLOT = PPlot.new
